import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/system/dict'

const api = {

    /**
     * 新增枚举
     * @param dict
     * @returns {Promise<*|undefined>}
     */
    createDict: (dict) =>
      axiosClient().post(`${requestUrl}/addInfo`, dict),

    /**
     * 根据ID删除枚举
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteDictById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新枚举
     * @param id
     * @param dict
     * @returns {Promise<*|undefined>}
     */
    updateDictFull: (id, dict) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, dict),

    /**
     * 根据ID部分更新枚举
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateDictPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询枚举
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getDictById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询枚举
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getDictPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询枚举
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getDictList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.dictCode) {
            params.append('dictCode', businessParam.dictCode)
        }
        if (businessParam.dictName) {
            params.append('dictName', businessParam.dictName)
        }
        if (businessParam.dictType) {
            params.append('dictType', businessParam.dictType)
        }
        if (businessParam.description) {
            params.append('description', businessParam.description)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
